package kitread.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.MatteBorder;

import kitread.model.Feed;
import kitread.model.FeedItem;
import kitread.model.FeedsCollection;
import kitread.ui.widgets.FeedItemCellRenderer;
import kitread.ui.widgets.FeedItemsListModel;
import kitread.util.HTMLGenerator;
import kitread.util.UIDecorator;

import javax.swing.JTextArea;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.plaf.LabelUI;
import javax.swing.text.StyleConstants;

import com.alee.laf.scroll.WebScrollBarUI;
import com.alee.laf.scroll.WebScrollPane;
import com.alee.laf.text.WebEditorPane;
import com.alee.laf.text.WebTextPane;

import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.Font;
import java.io.File;

import javax.swing.JTextPane;

import org.xhtmlrenderer.simple.XHTMLPanel;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.ScrollPaneConstants;


public class Main {

	private JFrame frmRssReader;
	private FeedsCollection feeds = new FeedsCollection();
	private XHTMLPanel pageContent;
	private JLabel labelDate;
	private JLabel labelTitle;
	private JLabel labelUrl;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					
					window.feeds.addFeed(new Feed("http://www.space.com/home/feed/site.xml"));
					window.feeds.addFeed(new Feed("http://xkcd.com/rss.xml"));
					window.feeds.addFeed(new Feed("http://phdcomics.com/gradfeed.php"));
					window.feeds.refresh();
					
					window.frmRssReader.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRssReader = new JFrame();
		frmRssReader.setTitle("RSS Reader");
		frmRssReader.setBounds(100, 100, 953, 616);
		frmRssReader.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{150, 350, 0, 0};
		gridBagLayout.rowHeights = new int[]{300, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		frmRssReader.getContentPane().setLayout(gridBagLayout);
		
		JPanel panel_feeds = new JPanel();
		panel_feeds.setBorder(new MatteBorder(0, 0, 0, 2, (Color) new Color(184, 184, 184)));
		panel_feeds.setBackground(new Color(206, 206, 206));
		GridBagConstraints gbc_panel_feeds = new GridBagConstraints();
		gbc_panel_feeds.fill = GridBagConstraints.BOTH;
		gbc_panel_feeds.gridx = 0;
		gbc_panel_feeds.gridy = 0;
		frmRssReader.getContentPane().add(panel_feeds, gbc_panel_feeds);
		
		JPanel panel_items = new JPanel();
		GridBagConstraints gbc_panel_items = new GridBagConstraints();
		gbc_panel_items.fill = GridBagConstraints.BOTH;
		gbc_panel_items.gridx = 1;
		gbc_panel_items.gridy = 0;
		frmRssReader.getContentPane().add(panel_items, gbc_panel_items);
		panel_items.setLayout(new BorderLayout(0, 0));
		
		
		final JList<FeedItem> list = new JList<FeedItem>();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				if (e.getValueIsAdjusting() == false) {
					if (list.getSelectedIndex() != -1) {
						new Thread() {
							@Override
							public void run() {
								FeedItem fi = list.getModel().getElementAt(list.getSelectedIndex());
								File content = HTMLGenerator.generateHTML(fi);
								labelUrl.setText(fi.getUrl().toString());
								labelTitle.setText(fi.getTitle());
								labelDate.setText(fi.getPublishingDate().toString());
								try {
									pageContent.setDocument(content);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						}.start();
						
					}
				}
			}
		});
		list.setModel(new FeedItemsListModel(feeds));
		list.setCellRenderer(new FeedItemCellRenderer());
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setOpaque(false);
		
		JScrollPane scrollPane = new WebScrollPane(list);
		scrollPane.setPreferredSize(new Dimension(350, 131));
		scrollPane.setBorder(null);
		panel_items.add(scrollPane, BorderLayout.WEST);
		
		JPanel panel_details = new JPanel();
		panel_details.setBackground(new Color(28, 28, 28));
		GridBagConstraints gbc_panel_details = new GridBagConstraints();
		gbc_panel_details.weightx = 1.0;
		gbc_panel_details.fill = GridBagConstraints.BOTH;
		gbc_panel_details.gridx = 2;
		gbc_panel_details.gridy = 0;
		frmRssReader.getContentPane().add(panel_details, gbc_panel_details);
		GridBagLayout gbl_panel_details = new GridBagLayout();
		gbl_panel_details.columnWidths = new int[]{361, 0};
		gbl_panel_details.rowHeights = new int[]{10, 0, 0, 0, 0};
		gbl_panel_details.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gbl_panel_details.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		panel_details.setLayout(gbl_panel_details);
		
		pageContent = new XHTMLPanel();
		pageContent.getSharedContext().getTextRenderer().setSmoothingThreshold(0);
		pageContent.setBackground(new Color(28, 28, 28));
		pageContent.setFont(new Font("Cambria", Font.PLAIN, 16));
		pageContent.setForeground(new Color(154, 165, 172));

		WebScrollPane scrollPane2 = new WebScrollPane(pageContent);
		scrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane2.getWebVerticalScrollBar().setOpaque(false);
		scrollPane2.getWebHorizontalScrollBar().setOpaque(false);
		UIDecorator.decorateDarkScrollBar(scrollPane2.getWebVerticalScrollBar());
		UIDecorator.decorateDarkScrollBar(scrollPane2.getWebHorizontalScrollBar());
		scrollPane2.setBorder(null);
		GridBagConstraints gbc_pageContent = new GridBagConstraints();
		gbc_pageContent.insets = new Insets(10, 10, 25, 5);
		gbc_pageContent.fill = GridBagConstraints.BOTH;
		gbc_pageContent.gridx = 0;
		gbc_pageContent.gridy = 3;
		panel_details.add(scrollPane2, gbc_pageContent);
		
		labelUrl = new JLabel("");
		labelUrl.setForeground(new Color(104, 102, 93));
		labelUrl.setFont(new Font("Dialog", Font.PLAIN, 11));
		GridBagConstraints gbc_labelUrl = new GridBagConstraints();
		gbc_labelUrl.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelUrl.insets = new Insets(25, 20, 10, 20);
		gbc_labelUrl.gridx = 0;
		gbc_labelUrl.gridy = 0;
		panel_details.add(labelUrl, gbc_labelUrl);
		
		labelTitle = new JLabel("");
		labelTitle.setForeground(new Color(255, 255, 255));
		labelTitle.setFont(new Font("Cambria", Font.PLAIN, 24));
		GridBagConstraints gbc_labelTitle = new GridBagConstraints();
		gbc_labelTitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelTitle.insets = new Insets(0, 20, 0, 20);
		gbc_labelTitle.gridx = 0;
		gbc_labelTitle.gridy = 1;
		panel_details.add(labelTitle, gbc_labelTitle);
		
		labelDate = new JLabel("");
		labelDate.setForeground(new Color(220, 170, 115));
		labelDate.setFont(new Font("Dialog", Font.PLAIN, 12));
		labelDate.setBorder(new CompoundBorder(new MatteBorder(0, 0, 3, 0, (Color) new Color(59, 59, 59)), new EmptyBorder(0, 0, 10, 0)));
		GridBagConstraints gbc_labelDate = new GridBagConstraints();
		gbc_labelDate.insets = new Insets(10, 20, 10, 20);
		gbc_labelDate.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelDate.gridx = 0;
		gbc_labelDate.gridy = 2;
		panel_details.add(labelDate, gbc_labelDate);
	}
}
